package com.tedu.egnotice2.controller;

import com.tedu.egnotice2.base.response.JsonResult;
import com.tedu.egnotice2.base.response.StatusCode;
import com.tedu.egnotice2.mapper.NoticeMapper;
import com.tedu.egnotice2.pojo.dto.NoticeAddParam;
import com.tedu.egnotice2.pojo.dto.NoticeAddQuery;
import com.tedu.egnotice2.pojo.dto.NoticeUpdateParam;
import com.tedu.egnotice2.pojo.entity.Notice;
import com.tedu.egnotice2.pojo.vo.NoticeDetailVO;
import com.tedu.egnotice2.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("/add")
    public JsonResult insert(@RequestBody NoticeAddParam noticeAddParam){
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(100l);
        int num = noticeMapper.insert(notice);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("/list")
    public JsonResult select(NoticeAddQuery noticeAddQuery){
        List<NoticeListVO> list = noticeMapper.select(noticeAddQuery);
        return JsonResult.success(list);
    }
    @GetMapping("/del")
    public JsonResult delete(Long id){
        int num = noticeMapper.delete(id);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("/detail")
    public JsonResult detail(Long id){
        NoticeDetailVO detailVO =  noticeMapper.selectDetail(id);
        return JsonResult.success(detailVO);
    }
    @PostMapping("/update")
    public JsonResult update(@RequestBody NoticeUpdateParam noticeUpdateParam){
        Notice notice = new Notice();
        notice.setUpdateTime(new Date());
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        int num = noticeMapper.update(notice);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
